import { useGlobalStore } from "@/store/global";


export function registerDirective(app) {
    app.directive('permission', {
        mounted(el, binding) {
            const globalStore = useGlobalStore();

            checkPermission(el, binding, globalStore);

            
            globalStore.$subscribe(() => {
                checkPermission(el, binding, globalStore);
            });
        }
    });
}

function checkPermission(el, binding, globalStore) {
    const permissionData = globalStore.permissionData;
    const result = findPermission(permissionData, binding.value);

    if (!result) {
        el.style.display = 'none';
     
    } else {
        el.style.display = ''; // 恢复显示
    
    }
}

function findPermission(permissionData, value) {
    return permissionData?.some(item => {
        if (item.children) {
            return findPermission(item.children, value);
        }
        return item.title == value;
    });
}
